<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="net.ifilm.entity.Film" %>
<%@ page import="java.util.List" %>
<%@ page import="net.ifilm.entity.PageCount" %>
<%@ page import="net.ifilm.utility.DateTimeUtil" %>
<%@ page import="net.ifilm.utility.StringUtil" %>
<%
    PageCount pageCount = (PageCount)request.getAttribute("pageCount");
%>
<html>
<head>
    <title></title>
    <link rel="stylesheet" href="<%=request.getContextPath() %>/bg/css/bg.css" type="text/css" />
    <link rel="stylesheet" href="<%=request.getContextPath() %>/css/jquery.autocomplete.css" type="text/css" />
    <script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-1.7.1.min.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery.autocomplete.min.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath() %>/js/ifilm.js"></script>
    <style type="text/css">
        #livetip {
            position: absolute;
            background-color: #dadada;
            padding: 4px;
            /*border: 1px solid #000000;*/
            border-radius: 4px;
            -webkit-border-radius: 4px;
            -moz-border-radius: 4px;
        }
        #filmTable tr:hover {
            background-color: #87ceeb;
        }
        td img { cursor:pointer; }
        th { cursor:pointer; }
        #searchPannel a { background: none repeat scroll 0 0 #808080; padding: 5px; }
        #searchPannel a:hover { background: none repeat scroll 0 0 #0184C8; color: #FFFFFF; text-decoration: none;}
        #searchPannel div { width: 100%;height: 30px; margin-top: 10px; display: none; }
    </style>
    <script type="text/javascript">
    $(function(){

        var $liveTip = $('<div id="livetip"></div>').hide().appendTo('body');
        var tipTitle = '';
        $('#filmTable').bind('mouseover mouseout mousemove', function(event) {
            var $link = $(event.target).closest('a');
            if (!$link.length) { return; }
            var link = $link[0];
            if (event.type == 'mouseover' || event.type == 'mousemove') {
                $liveTip.css({
                    top: event.pageY + 12,
                    left: event.pageX + 12
                });
            }
            if (event.type == 'mouseover') {
                tipTitle = link.title;
                link.title = '';

                var arr = tipTitle.split('|')
                $liveTip.html(
                        '<div>'
                        + '<div style="float: left;">'
                        + '<img alt="' + arr[0] + '" src="' + arr[0] + '" width="70" height="100"/>'
                        + '</div>'
                        + '<div style="float: right;padding-left: 5px;padding-top: 3px;">'
                        + '<b>电影名称</b>：' + arr[6] + '<br/>'
                        + '<b>电影类型</b>：' + arr[4] + '<br/>'
                        + '<b>影视明星</b>：' + arr[3] + '<br/>'
                        + '<b>播放时长</b>：' + arr[5] + '<br/>'
                        + '<b>相关标签</b>：' + splitStr(arr[2], ';') + '<br/>'
                        + '<b>电影简介</b>：' + cutoutStr(arr[1], 30)
                        + '</div>'
                        + '</div>').show();
            };
            if (event.type == 'mouseout') {
                $liveTip.hide();
                if (tipTitle) {
                    link.title = tipTitle;
                }
            }
        });

        $("input[name='searchInput']").bind("input keydown blur focus", function(event) {
            var searchInput = $("input[name='searchInput']");
            if (event.type == 'input') {
                var value = searchInput.val();
                if (value == null || value == "") return;

                var width = $("input[name='searchInput']").width();
                width = width + 5;

                $("input[name='searchInput']").AutoComplete({
                    'data': "autoFilmName.html",
                    'ajaxParams':{'searchKey': value},
                    'ajaxDataType': 'xml',
                    'width': width,
                    'maxItems': 20,
                    'ajaxType':'POST',
                    'async': true,
                    'onerror': function(msg){  }
                });
            } else if (event.type == 'keydown') {
                if (e.keyCode == 13) { submitSearchFilm(); }
            } else if (event.type == 'blur') {
                if (searchInput.val() == null || searchInput.val() == "") searchInput.val("请输入电影标题搜索关键字")
            } else if (event.type == 'focus') {
                if (searchInput.val() == '请输入电影标题搜索关键字') searchInput.val("");
            }
        });

        $("a:contains('确定')").bind("click", function() { submitSearchFilm(); });

        $("th:contains('上映年份')").bind("click", function() {
            location.href = 'filmList.html?page=<%=pageCount.getCurrentPage()%>&sortColumn=showtime';
        });
        $("th:contains('启用?')").bind("click", function() {
            location.href = 'filmList.html?page=<%=pageCount.getCurrentPage()%>&sortColumn=activeflag';
        });
        $("th:contains('限制?')").bind("click", function() {
            location.href = 'filmList.html?page=<%=pageCount.getCurrentPage()%>&sortColumn=islimit';
        });
        $("th:contains('高清?')").bind("click", function() {
            location.href = 'filmList.html?page=<%=pageCount.getCurrentPage()%>&sortColumn=isclear';
        });
        $("th:contains('创建时间')").bind("click", function() {
            location.href = 'filmList.html?page=<%=pageCount.getCurrentPage()%>&sortColumn=createtime';
        });
        $("th:contains('播放次数')").bind("click", function() {
            location.href = 'filmList.html?page=<%=pageCount.getCurrentPage()%>&sortColumn=playnum';
        });
        $("th:contains('ID')").bind("click", function() {
            location.href = 'filmList.html?page=<%=pageCount.getCurrentPage()%>&sortColumn=filmid';
        });

    }); // end of $(function()

    function selectAll(chk_all) {
        var chk_list = document.getElementsByName('chk_list');
        for (var i = 0; i < chk_list.length; i++) {
            chk_list[i].checked = chk_all.checked;
        }
    }

    function delFilmType() {
        var chk_list = document.getElementsByName('chk_list');

        var i;
        for (i = 0; i < chk_list.length; i++) {
            if (chk_list[i].checked == true) break;
        }

        if (i == chk_list.length) {
            showErrorMsg(document.getElementById('message'), "请至少选择一个目标进行删除！");
            return;
        }

        var idList = "";
        var imagePath = "";
        for (i = 0; i < chk_list.length; i++) {
            if (chk_list[i].checked == true) {
                idList = idList + chk_list[i].value;
                imagePath = imagePath + chk_list[i].title;
                if (i != chk_list.length - 1) {
                    idList = idList + ',';
                    imagePath = imagePath + ',';
                }
            }
        }

        location.href = "batchDelFilm.html?filmIdList="
                + idList + "&imagePathList=" + imagePath + "&currentPage=<%=pageCount.getCurrentPage()%>";
    }

    function testPlay(filmId) {
        var iTop = (window.screen.availHeight-30-685)/2; //获得窗口的垂直位置;
        var iLeft = (window.screen.availWidth-10-515)/2; //获得窗口的水平位置;

        var answer = window.showModalDialog('testPlay.html?filmId=' + filmId, null, 'dialogHeight:685px;dialogWidth:515px;'
                + 'dialogLeft:' + iLeft + ';dialogTop:' + iTop
                + ';center:yes;help:no;scrollbars:no;resizable:no;location:no;status:no');
        alert(answer);
    }

    function changeFilmStatus(filmId, activeFlag, limit, hot, clear, showTime, regionId) {
        $.ajax({type:'GET', url:'changeFilmStatus.html',
            data:'filmId=' + filmId + '&activeFlag=' + activeFlag + '&limit='
                    + limit + '&hot=' + hot + '&clear=' + clear + '&showTime=' + showTime + '&regionId=' + regionId});
    }

    function listRegion(selectRegion) {
        $.ajax({type:'get', url:'listFilmRegion.html', success:function(data) {
            // 清空节点所有元素
            while (selectRegion.firstChild) {
                var oldNode = selectRegion.removeChild(selectRegion.firstChild);
                oldNode = null;
            }
            $(data).find("param").each(function(){
                var id = $(this).attr("id");
                var regionname = $(this).attr("regionname");

                var option = document.createElement("option");
                option.value = id;
                option.innerHTML = regionname;
                selectRegion.appendChild(option);
            });
        }});
    }

    function listShowTime(selectShowTime) {
        $.ajax({type:'get', url:'listShowTime.html', success:function(data) {
            // 清空节点所有元素
            while (selectShowTime.firstChild) {
                var oldNode = selectShowTime.removeChild(selectShowTime.firstChild);
                oldNode = null;
            }

            $(data).find("param").each(function(){
                var year  = $(this).attr("value");

                var option = document.createElement("option");
                option.value = year;
                option.innerHTML = year;
                selectShowTime.appendChild(option);
            });
        }});
    }

    function submitSearchFilm() {
        var searchInput = $("input[name='searchInput']");
        if (searchInput.val() == '请输入电影标题搜索关键字') searchInput.val("");

        document.searchForm.action = "filmListByKey.html";
        document.searchForm.submit();
    }

        function showFilmDetail(filmId) {
            var iTop = (window.screen.availHeight-30-400)/2; //获得窗口的垂直位置;
            var iLeft = (window.screen.availWidth-10-700)/2; //获得窗口的水平位置;

            var answer = window.showModalDialog('filmDetail.html?filmId=' + filmId, null, 'dialogHeight:400px;dialogWidth:900px;'
                    + 'dialogLeft:' + iLeft + ';dialogTop:' + iTop
                    + ';center:yes;help:no;scrollbars:no;resizable:no;location:no;status:no');

            // 获取子窗口返回的值
            if (answer == 1) {
                window.location.href = 'filmList.html?page=<%=pageCount.getCurrentPage()%>';
//                window.location.reload();
//                window.location.reload(true);
            }
        }
    </script>
</head>
<body>

<div id="listDiv">
    <div id="message"></div>

    <form name="searchForm" method="POST">
        <div id="searchPannel">
            <img src="/images/search.png">
            <span>搜索：</span>
            <input type="text" size="50" name="searchInput" value="请输入电影标题搜索关键字"/>
            <a href="javascript:void(0);">确定</a>
        </div>
    </form>

    <table id="filmTable">

            <thead>
            <tr>
                <th><input type="checkbox" onclick="selectAll(this);"/></th>
                <th>ID</th>
                <th>电影标题</th>
                <th>上映年份</th>
                <th>限制?</th>
                <th>热门?</th>
                <th>高清?</th>
                <th>播放次数</th>
                <th align="center">地区</th>
                <th>创建时间</th>
                <th>启用?</th>
                <th>控制</th>
            </tr>
            </thead>
            <tbody>
                <%
                    List<Film> filmList = (List<Film>)request.getAttribute("filmList");
                    for (Film film : filmList) {
                %>
                    <tr>
                        <td align="center">
                            <input type="checkbox" name="chk_list" value="<%=film.getFilmId()%>" title="<%=film.getImagePath()%>"/>
                        </td>
                        <td align="center" style="background-color: #808080;"><%=film.getFilmId()%></td>
                        <td>
                            <a href="javascript:showFilmDetail('<%=film.getFilmId()%>');"
                               title="<%=request.getContextPath() + film.getImagePath()%>|<%=film.getFilmIntroduce()%>|
                                <%=film.getTags()%>|
                                <%=StringUtil.formatStarList(film.getStarList())%>|
                                <%=StringUtil.formatFilmTypeList(film.getFilmTypeList())%>|
                                <%=film.getFilmTime()%>|<%=film.getFilmName()%>">
                                <%=film.getFilmName()%>
                                <%=film.getSlideFilm() != null? "<img src='/images/isslide.png'/>":""%>
                            </a>
                        </td>
                        <td align="center">
                            <select onmousedown="listShowTime(this);" onchange="changeFilmStatus(<%=film.getFilmId()%>,
                                    <%=film.getActiveFlag()%>, <%=film.getIslimit()%>, <%=film.getHot()%>, <%=film.getClear()%>,
                                    this.value, <%=film.getRegionId()%>);">
                                <option value="<%=film.getShowTime()%>"><%=film.getShowTime()%></option>
                            </select>
                        </td>
                        <td align="center">
                            <select onchange="changeFilmStatus(<%=film.getFilmId()%>, <%=film.getActiveFlag()%>
                                    , this.value, <%=film.getHot()%>, <%=film.getClear()%>, <%=film.getShowTime()%>,
                                    <%=film.getRegionId()%>);">
                                <option value="0" <%=film.getIslimit()==0?"selected":""%>>是</option>
                                <option value="1" <%=film.getIslimit()==1?"selected":""%>>否</option>
                            </select>
                        </td>
                        <td align="center">
                            <select onchange="changeFilmStatus(<%=film.getFilmId()%>, <%=film.getActiveFlag()%>
                                    , <%=film.getIslimit()%>, this.value, <%=film.getClear()%>, <%=film.getShowTime()%>,
                                    <%=film.getRegionId()%>);">
                                <option value="0" <%=film.getHot()==0?"selected":""%>>是</option>
                                <option value="0" <%=film.getHot()==1?"selected":""%>>否</option>
                            </select>
                        </td>
                        <td align="center">
                            <select onchange="changeFilmStatus(<%=film.getFilmId()%>, <%=film.getActiveFlag()%>
                                    , <%=film.getIslimit()%>, <%=film.getHot()%>, this.value, <%=film.getShowTime()%>,
                                    <%=film.getRegionId()%>);">
                                <option value="0" <%=film.getClear()==0?"selected":""%>>DVD：普通清晰版</option>
                                <option value="1" <%=film.getClear()==1?"selected":""%>>BD：高清无水印版</option>
                                <option value="2" <%=film.getClear()==2?"selected":""%>>HD：高清版</option>
                                <option value="3" <%=film.getClear()==3?"selected":""%>>TS：抢先非清晰版</option>
                            </select>
                        </td>
                        <td align="right"><%=film.getPlayNum()%></td>
                        <td align="center">
                            <select onmousedown="listRegion(this);" onchange="changeFilmStatus(<%=film.getFilmId()%>,
                                    <%=film.getActiveFlag()%>, <%=film.getIslimit()%>, <%=film.getHot()%>,
                                    <%=film.getClear()%>, <%=film.getShowTime()%>, this.value);">
                                <option value="<%=film.getRegionId()%>"><%=film.getRegion().getRegionName()%></option>
                            </select>
                        </td>
                        <td align="right"><%=DateTimeUtil.parseCreateTime(film.getCreateTime())%></td>
                        <td align="center">
                            <select onchange="changeFilmStatus(<%=film.getFilmId()%>, this.value
                                    , <%=film.getIslimit()%>, <%=film.getHot()%>, <%=film.getClear()%>,
                                <%=film.getShowTime()%>, <%=film.getRegionId()%>);">
                                <option value="0" <%=film.getActiveFlag()==0?"selected":""%>>是</option>
                                <option value="1" <%=film.getActiveFlag()==1?"selected":""%>>否</option>
                            </select>
                        </td>
                        <td align="center">
                            <img class="someClass" title="测试播放此电影" src="/images/player.png"
                                 onclick="javascript:testPlay(<%=film.getFilmId()%>);" />
                            <img class="someClass" title="删除此条电影纪录" src="/images/delete.png"
                                 onclick="location.href='delFilm.html?filmId=<%=film.getFilmId()%>&imagePath=<%=film.getImagePath()%>&page=<%=pageCount.getCurrentPage()%>';"/>
                        </td>
                    </tr>
                <%
                    }
                %>
            </tbody>
         </table>

    <div id="pannel">
        <a href="javascript:delFilmType();" class="someClass" title="批量删除电影纪录">-</a>
        <a href="javascript:location.href='filmMgr.htm';" class="someClass" title="增加新的电影纪录">+</a>
    </div>


    <div id="control">
        <%
            int currentPage = pageCount.getCurrentPage();
            int totalPages = pageCount.getTotalPage();
        %>
        <a href="javascript:location.href='filmList.html?page=1'" class="someClass" title="跳转到首页"><<</a>
        <% if (currentPage == 1) { %>
        <a href="javascript:void(0);" style="background-color: #808080;"><</a>
        <% } else { %>
        <a href="javascript:location.href='filmList.html?page=<%=currentPage - 1%>'" class="someClass" title="跳转到上一页"><</a>
        <% } %>
        <label><%=currentPage%>,<%=totalPages%> 页</label>
        <% if (currentPage == totalPages){ %>
        <a href="javascript:void(0);" style="background-color: #808080;">></a>
        <% } else { %>
        <a href="javascript:location.href='filmList.html?page=<%=currentPage + 1%>'" class="someClass" title="跳转到下一页">></a>
        <% } %>
        <a href="javascript:location.href='filmList.html?page=<%=totalPages%>'" class="someClass" title="跳转到尾页">>></a>

        <div>
            跳转到
            <select onchange="location.href = 'filmList.html?page=' + this.value">
                <% for (int i = 1; i <= totalPages; i++) { %>
                    <option value="<%=i%>" <%=currentPage==i?"selected":""%>><%=i%>页</option>
                <% } %>
            </select>
        </div>
    </div>

</div>



</body>
</html>